package home.control.service;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.managment.data.Media;
import com.managment.data.Profile;
import com.managment.data.Task;

import home.control.logicElements.Action;
import home.control.logicElements.ActionTypes;
import home.control.logicElements.HomeParts;
import home.control.logicElements.Plan;

public class Converter {
	public static String actionTypesToState(ActionTypes a){
		String ret = null;
		if(a == ActionTypes.HEATING_18){
			ret = "18";
		} else if(a == ActionTypes.HEATING_20){
			ret = "20";
		} else if(a == ActionTypes.HEATING_22){
			ret = "22";
		} else if(a == ActionTypes.HEATING_24){
			ret = "24";
		} else if(a == ActionTypes.HEATING_OFF){
			ret = "0";
		} else if(a == ActionTypes.TURN_OFF_LIGHTS){
			ret = "off";
		} else if(a == ActionTypes.TURN_ON_LIGHTS){
			ret = "on";
		}
		return ret;
	}
	
	public static ActionTypes stateToActionTypes(String s){
		ActionTypes at = null;
		if(s.equals("18")){
			at = ActionTypes.HEATING_18;
		} else if(s.equals("20")){
			at = ActionTypes.HEATING_20;
		} else if(s.equals("22")){
			at = ActionTypes.HEATING_22;
		} else if(s.equals("24")){
			at = ActionTypes.HEATING_24;
		} else if(s.equals("0")){
			at = ActionTypes.HEATING_OFF;
		} else if(s.equals("off")){
			at = ActionTypes.TURN_OFF_LIGHTS;
		} else if(s.equals("on")){
			at = ActionTypes.TURN_ON_LIGHTS;
		}
		return at;
	}
	
	public static Media homePartsToMedia(HomeParts hp){
		Media m = null;
		if(hp == HomeParts.LIVING_LIGHTS){
			m = Media.ROOMLIGHT;
		} else if(hp == HomeParts.KITCHEN_LIGHTS){
			m = Media.KITCHENLIGHT;
		} else if(hp == HomeParts.BATHROOM_LIGHTS){
			m = Media.BATHROOMLIGHT;
		} else if(hp == HomeParts.ALL_HOUSE){
			m = Media.HEATTER;
		}
		return m;
	}
	
	public static HomeParts mediaToHomeParts(Media m){
		HomeParts ret = null;
		if(m == Media.ROOMLIGHT){
			ret = HomeParts.LIVING_LIGHTS;
		} else if(m == Media.KITCHENLIGHT){
			ret = HomeParts.KITCHEN_LIGHTS;
		} else if(m == Media.BATHROOMLIGHT){
			ret = HomeParts.BATHROOM_LIGHTS;
		} else if(m == Media.HEATTER){
			ret = HomeParts.ALL_HOUSE;
		}
		return ret;
	}
	
	public static Task actionToTask(Action a){
		Time eventTime = new Time(a.getTime().getTime());
		Media media = homePartsToMedia(a.getPartOfHome());
		String state = actionTypesToState(a.getAction());
		return new Task(media, eventTime, state);
	}
	
	public static Action taskToAction(Task t){ 
		ActionTypes action = stateToActionTypes(t.getState()); 
		HomeParts partOfHome = mediaToHomeParts(t.getMedia());
		Date time = t.getEventTime();
		return new Action(partOfHome, action, time);
	}
	
	public static Plan profileToPlan(Profile pf){
		Plan p = new Plan();
		boolean isActive = pf.isActive();
		String name = pf.getName();
		List<Action> actions = new ArrayList<Action>();
		for(Task t : pf.getTasks()){
			actions.add(taskToAction(t));
		}
		
		p.setActive(isActive);
		p.setName(name);
		p.setActions(actions);
		return p;
	}
	
	public static Profile planToProfile(Plan p){
		Profile pf = new Profile();
		boolean active = p.isActive();
		String name = p.getName();
		List<Task> tasks = new ArrayList<Task>();
		for(Action a : p.getActions()){
			tasks.add(actionToTask(a));
		}
		
		pf.setActive(active);
		pf.setName(name);
		pf.setTasks(tasks);
		
		return pf;
	}
	
	public static List<Plan> profileListToPlanList(List<Profile> l){
		List<Plan> plans = new ArrayList<Plan>();
		for(Profile p : l){
			plans.add(profileToPlan(p));
		}
		return plans;
	}
	
	public static List<Profile> planListToProfileList(List<Plan> l){
		List<Profile> profiles = new ArrayList<Profile>();
		for(Plan p : l){
			profiles.add(planToProfile(p));
		}
		return profiles;
	}
}
